package com.qujia.integral.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qujia.common.annotation.Log;
import com.qujia.common.constant.UserConstants;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.model.LoginUser;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.enums.BusinessType;
import com.qujia.common.exception.CustomException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.poi.ExcelUtil;
import com.qujia.integral.domain.IntegralOrder;
import com.qujia.integral.service.IIntegralOrderService;
import com.qujia.system.domain.SysEnterpriseUser;
import com.qujia.system.domain.vo.SysEnterpriseAndStationVo;
import com.qujia.system.service.ISysDeptService;
import com.qujia.system.service.ISysEnterpriseUserService;
import com.qujia.system.service.ISysStationUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 积分消费订单Controller
 *
 * @author qujia
 * @date 2021-02-24
 */
@RestController
@RequestMapping("/integral/order")
public class IntegralOrderController extends BaseController {
    @Autowired
    private IIntegralOrderService integralOrderService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private ISysStationUserService stationUserService;
    @Autowired
    private ISysEnterpriseUserService enterpriseUserService;

    /**
     * 查询积分消费订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(IntegralOrder integralOrder) {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if ("1".equals(currentUser.getUser().getUserLevel())) {
            integralOrder.setEnterpriseId(currentUser.getUser().getEnterpriseId());
//            List<String> list = stationUserService.selectStationIdByEnterpriseId(currentUser.getUser().getEnterpriseId());
//            if (list != null && list.size() > 0) {
//                integralOrder.setStationIdList(list);
//                integralOrder.setStationId(null);
//            }
        } else if ("2".equals(currentUser.getUser().getUserLevel())) {
            integralOrder.setStationId(currentUser.getUser().getStationId());
        } else if (SecurityUtils.isPartner()) {
            SysEnterpriseAndStationVo itemIds = stationUserService.getItemIds(currentUser.getUser());
            if (itemIds != null){
                if(CollectionUtils.isEmpty(itemIds.getEnterpriseIds()) && CollectionUtils.isEmpty(itemIds.getStationIdsStr())){
                    return getDataTable(new ArrayList<>());
                }
                integralOrder.setEnterpriseIdListForAuthor(itemIds.getEnterpriseIds());
                integralOrder.setStationIdList(itemIds.getStationIdsStr());
            } else {
                throw new CustomException(UserConstants.NO_ENTERPRISE);
            }
        }
        startPage();
        List<IntegralOrder> orderList = integralOrderService.selectIntegralOrderList(integralOrder);
        return getDataTable(orderList);
    }

    /**
     * 导出积分消费订单列表
     */
    @Log(title = "积分消费订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(IntegralOrder integralOrder) {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        if ("1".equals(currentUser.getUser().getUserLevel())) {
            List<String> list = stationUserService.selectStationIdByEnterpriseId(currentUser.getUser().getEnterpriseId());
            if (list != null && list.size() > 0) {
                integralOrder.setStationIdList(list);
                integralOrder.setStationId(null);
            }
        } else if ("2".equals(currentUser.getUser().getUserLevel())) {
            integralOrder.setStationId(currentUser.getUser().getStationId());
        }
        List<IntegralOrder> orderList = integralOrderService.selectIntegralOrderList(integralOrder);
        ExcelUtil<IntegralOrder> util = new ExcelUtil<IntegralOrder>(IntegralOrder.class);
        return util.exportExcel(orderList, "order");
    }

    /**
     * 获取积分消费订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(integralOrderService.selectIntegralOrderById(id));
    }

    /**
     * 新增积分消费订单
     */
    @Log(title = "积分消费订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IntegralOrder integralOrder) {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        integralOrder.setStationId(currentUser.getUser().getStationId());
        integralOrder.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(integralOrderService.insertIntegralOrder(integralOrder));
    }

    /**
     * 修改积分消费订单
     */
    @Log(title = "积分消费订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IntegralOrder integralOrder) {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        integralOrder.setStationId(currentUser.getUser().getDeptId());
        integralOrder.setStationName(currentUser.getUser().getDept().getDeptName());
        return toAjax(integralOrderService.updateIntegralOrder(integralOrder));
    }

    /**
     * 删除积分消费订单
     */
    @Log(title = "积分消费订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(integralOrderService.deleteIntegralOrderByIds(ids));
    }
}
